/**
 * 
 */
package com.syxp.pda.action.base;

import java.util.List;

import javax.annotation.Resource;

import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.syxp.pda.core.bean.FieldBean;
import com.syxp.pda.core.command.QueryFilter;
import com.syxp.pda.core.util.Operation;
import com.syxp.pda.core.web.PagingBean;
import com.syxp.pda.core.web.action.BaseAction;
import com.syxp.pda.entity.base.Duty;
import com.syxp.pda.service.base.DutyService;

/**
 * @author Administrator
 *
 */
public class DutyAction extends BaseAction {
	@Resource
	private DutyService dutyService;
	private Duty duty;
	private int departmentID ;  //获取部门的信息
	private int dutyID;
	private int start ;
	
	private int limit ;
	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLimit() {
		return limit;
	}

	public void setLimit(int limit) {
		this.limit = limit;
	}

	public DutyService getDutyService() {
		return dutyService;
	}

	public void setDutyService(DutyService dutyService) {
		this.dutyService = dutyService;
	}
	


	public int getDutyID() {
		return dutyID;
	}

	public void setDutyID(int dutyID) {
		this.dutyID = dutyID;
	}

	public int getDepartmentID() {
		return departmentID;
	}

	public void setDepartmentID(int departmentID) {
		this.departmentID = departmentID;
	}

	public Duty getDuty() {
		return duty;
	}

	public void setDuty(Duty duty) {
		this.duty = duty;
	}
	
	public String load(){ //filter是过滤查询条件的~~~
		try{
			List<Duty> all=null;
			QueryFilter filter=new QueryFilter();
			if(departmentID==0){
				if(limit==0){
					all=dutyService.getDao().getAll();
				}else{
					filter.setPagingBean(new PagingBean(start,limit)) ;
					all=dutyService.getDao().getAll(filter);
				}
				
			}else{
				filter.setPagingBean(new PagingBean(start,limit)) ;
				filter.addFilter(new FieldBean("departmentID" ,departmentID ,Operation.EQUAL));
				all = dutyService.getDao().getAll(filter);
			}
			JsonArray jarray=new JsonArray();
			JsonObject result = new JsonObject() ;
			for(Duty duty : all){
				JsonObject jobj=new JsonObject();
				jobj.addProperty("dutyID", String.valueOf(duty.getDutyID()));
				jobj.addProperty("name", duty.getDutyName());
				jobj.addProperty("remarks", duty.getRemarks());
				jarray.add(jobj);
			}
			if(limit==0){
				this.jsonString = gson.toJson(jarray) ;
			}else{
				result.addProperty("totalProperty",dutyService.getDao().getTotalProperty(filter)) ;
				result.add("result", jarray) ;
				result.addProperty("start", start);
				result.addProperty("limit", limit) ;
				this.jsonString="{success:true}" ;
				this.jsonString = gson.toJson(result) ;
			}
			
			System.out.println(jsonString) ;
		}catch(Exception e){
			e.printStackTrace();
			
		}
		return SUCCESS;
	}
	
	public String save(){
		try{
			System.out.println(duty.getDepartmentID());
			Duty d=dutyService.addService(duty);
			JsonObject jobj=new JsonObject();
			jobj.addProperty("id", String.valueOf(duty.getDutyID()));
			jobj.addProperty("name", duty.getDutyName());
			jobj.addProperty("remark", duty.getRemarks());
			jobj.addProperty("success", true) ;
			this.jsonString=super.gson.toJson(jobj) ;
			return SUCCESS;
		}catch(Exception e){
			e.printStackTrace();
			return SUCCESS;
		}
		
	}
	
	public String delete() {  //删除
		try{
			System.out.println(dutyID+"sddddddddddddd");
			dutyService.removeService(dutyID) ;
			this.jsonString="{success:true}" ;
		}catch(Exception e){
			e.printStackTrace() ;
			this.jsonString="{success:false}" ;
		}
		
		return SUCCESS ;
	}
	
	public String update() {
		try{
			System.out.println(duty.getDutyID()+"kkkkkkkkkkkkk");
			dutyService.updateService(duty) ;
			this.jsonString = "{success:true}" ;
		}catch(Exception e) {
			e.printStackTrace() ;
			this.jsonString = "{success:false}" ;
		}
		return SUCCESS ;
	}
}
